package sk.stancel.peter.sudoku;

import java.util.List;

import sk.stancel.peter.sudoku.R;
import sk.stancel.peter.sudoku.core.Tile;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TileAdapter extends ArrayAdapter<Tile>{

	private List<Tile> listOfTiles;
	private Context context;
	
	public TileAdapter(Context context, List<Tile> listOfTiles) {
		super(context, 0, listOfTiles);
		this.listOfTiles = listOfTiles;
		this.context = context;
	}
	
	@Override
	public int getCount() {
		return listOfTiles.size();
	}

	@Override
	public Tile getItem(int position) {
		return listOfTiles.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, android.view.ViewGroup parent) {
		Tile tile = listOfTiles.get(position);
		View customView = LayoutInflater.from(context).inflate(R.layout.tiles, null);

		TextView tv = (TextView) customView.findViewById(R.id.text_tile);
		LinearLayout ll = (LinearLayout) customView.findViewById(R.id.layout_hints);
		
		tv.setTextSize(context.getSharedPreferences("preferences", Context.MODE_PRIVATE).getInt("sudoku_preferences_font_size", 1));

		int value = tile.getValue();
		if(value != 0){
			tv.setVisibility(View.VISIBLE);
			tv.setText(value+"");
			if(!tile.isChange()){
				tv.setTextColor(Color.RED);
			}
		}else {
			ll.setVisibility(View.VISIBLE);
		}
		
		int intHintSize = context.getSharedPreferences("preferences", Context.MODE_PRIVATE).getInt("sudoku_preferences_font_size_hint", 1);
		int k = 0;
		for (int i = 0; i < 3; i++) {
			LinearLayout llHint = (LinearLayout) ll.getChildAt(i);
			for (int j = 0; j < 3; j++) {
				TextView hint = (TextView) llHint.getChildAt(j);
				hint.setTextSize(intHintSize);
				if(tile.getHints()[k].isShow()){
					hint.setVisibility(View.VISIBLE);
				}
				k++;
			}
		}
		customView.setBackgroundResource(Sudoku.getBackgroundImageID(position, value));
		return customView;
	}
}